<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\ChannelCollectionService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminJWTAuthMiddleware;

/**
 * Class ChannelCollectionController
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/channelCollection")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class ChannelCollectionController extends AdminBaseController {

    /**
     * @Inject()
     * @var ChannelCollectionService
     */
    protected $ChannelCollectionService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 详情
     * @PostMapping(path = "get")
     */
    public function get (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id' => $request->post('id'),
        ];

        return $this->ChannelCollectionService->get($param);
    }

    /**
     * 过滤添加/编辑参数
     *
     * @param RequestInterface $request
     *
     * @return array
     */
    protected function _filterEditParam (RequestInterface $request) {

        $param = [
            'collectionName' => $request->post('collectionName'),
            'conn'           => $request->post('conn', []),
        ];

        return $param;
    }

    /**
     * 添加
     * @PostMapping(path = "add")
     */
    public function add (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'collectionName' => 'required',
            'payType'        => 'required',
            // 'conn' => 'required',
        ]);

        $param = $this->_filterEditParam($request);

        $param['payType'] = $request->post('payType');

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelCollectionService->add($param);
    }

    /**
     * 编辑
     * @PostMapping(path = "edit")
     */
    public function edit (RequestInterface $request) {

        // 不允许编辑payType

        $this->_validOrFail($request->all(), [
            'id'             => 'required',
            'collectionName' => 'required',
            // 'conn' => 'required',
        ]);

        $param = $this->_filterEditParam($request);

        $param['id'] = $request->post('id');

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelCollectionService->edit($param);
    }

    // /**
    //  * 删除
    //  * @PostMapping(path = "del")
    //  */
    // public function del (RequestInterface $request) {
    //
    //     $this->_validOrFail($request->all(), [
    //         'id' => 'required',
    //     ]);
    //
    //     $param = [
    //         'id' => $request->post('id'),
    //     ];
    //
    //     return $this->ChannelCollectionService->del($param);
    // }

    /**
     * 列表
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        $param = [
            'page'  => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'collectionName');

        return $this->ChannelCollectionService->list($param);
    }

    /**
     * 列表下拉框
     * @PostMapping(path = "options")
     */
    public function options (RequestInterface $request) {

        $this->_validOrFail($request->all(), []);

        $param = [
            'search'  => $request->post('search'),
            'payType' => $request->post('payType'),
            // 'canPay' => $request->post('canPay'),
            // 'canRefund' => $request->post('canRefund'),
            // 'canTransfer' => $request->post('canTransfer'),
        ];

        return $this->ChannelCollectionService->options($param);
    }

    /**
     * 列表下拉框
     * @PostMapping(path = "settleCurrencyList")
     */
    public function settleCurrencyList (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id'  => $request->post('id'),
        ];

        return $this->ChannelCollectionService->settleCurrencyList($param);
    }

}
